Page({
	data: {
		nickName: 'name',
		avatarUrl: '/icon/225默认头像.png',
		showDialog: false,
		isLogin: true,
		isClear:false,
		showModalInput: false,
		newUserName:'',
		inputShowed: true,
	},
	chooseAvatar: function(event) {
		const {
			avatarUrl
		} = event.detail
		this.setData({
			avatarUrl: avatarUrl,
			// isLogin:false
		})
		wx.setStorageSync('avatarUrl', avatarUrl );
		wx.request({
      url: 'http://localhost:8001/user/setUser',
      method: 'POST',
      data: {
        userName: this.data.nickName,
				photo:this.data.avatarUrl
      },
      success: function(res) {
        console.log('修改成功');
      },
      fail: function(err) {
        console.error('修改失败', err);
      }
    });
		this.setData({
			// avatarUrl: avatarUrl,
			isLogin:false
		})
		wx.showToast({
				title: '登录成功',
				duration:2000
			})
	},
	
	wxLogin: function() {
		this.setData({
			isLogin:false,
			showModalInput:true,
		})
  },
	showInputBox: function () {
		this.setData({
			showModalInput: true,
			newUserName: this.data.nickName
		});
	},
	confirmInput: function (res) {
		let newName = this.data.newUserName;
		console.log(newName+666)
		if (newName === '') {
			wx.showToast({
				title: '用户名不能为空',
				icon: 'none',
				duration: 2000
			});
			return;
		};
		this.setData({
			nickName: newName,
			showModalInput: false,
			newnickName:newName
		});
		wx.setStorageSync('SetnickName', this.data.nickName);
		console.log(this.data.nickName)
	},

	inputChange: function (e) {
		this.setData({
			newUserName: e.detail.value
		});
	},
	onSubmit(event) {
		const	{nickName} = event.detail.value
		this.setData({
			nickName:nickName
		})
		// wx.setStorageSync('SetnickName', nickName);
	},
	
	navigateToSetting: function () {
		wx.navigateTo({
			url: '/pages/Setting/settings',
			events:{
				currentevent:(res)=>{
					console.log("res");
				}
			},
			success: function (res) {
				console.log("成功跳转到设置页面");
			},
			fail: function (err) {
				console.error("跳转到设置页面失败", err);
			}
		});
	},
	navigateTosaving: function () {
		wx.navigateTo({
			url: '/pages/saving/saving',
			success: function (res) {
				console.log("成功跳转到保存的记录页面");
			},
			fail: function (err) {
				console.error("跳转到保存的记录页面失败", err);
			}
		});
	},
	navigateToFeedback: function () {
		wx.navigateTo({
			url: '/pages/feedback/feedback',
			success: function (res) {
				console.log("成功跳转到问题反馈页面");
			},
			fail: function (err) {
				console.error("跳转到问题反馈页面失败", err);
			}
		});
	},
	showClearRecordsModal: function () {
		wx.showModal({
			title: '确认清空记录',
			content: '您确定要清空所有记录吗？',
			cancelText: '取消',
			confirmText: '确认',
			success: function (res) {
				if (res.confirm) {
					this.setData({
						isClear:true
					})
					
					console.log('用户点击了确认');
					wx.showLoading({
						title: '正在清空...',
						mask: true
					});
					wx.request({
						url: 'http://localhost:8001/record/clearRecords',
						method:'POST',
		       	data:{
                isClear:this.data.isClear
		       	}
					})
				} else if (res.cancel) {
					console.log('用户点击了取消');
				}
			},

		});
		wx.hideLoading()
	},

	onLoad(){
		let avatarUrl = this.data.avatarUrl;
		let nickName = this.data.nickName;
		this.setData({
			avatarUrl:avatarUrl,
			nickName:nickName
		})
		wx.request({
    url: 'http://localhost:8001/user/setUser',
    method: 'GET',
    success: (res) => {  
        console.log('请求成功');
        this.setData({
            nickName: res.data.userName,
            avatarUrl: res.data.photo
        });
    },
    fail: (err) => { 
        console.error('请求失败', err);
    }
  });
	  wx.request({
						url: 'http://localhost:8001/record/clearRecords',
						method:'GET',
						success: (res) =>{
							console.log('请求成功');
              this.setData({
                 isClear:res.data.isClear
              });
						}
					})
	wx.setStorageSync('avatarUrl', this.data.newavatarUrl);
	wx.setStorageSync('SetnickName', this.data.newnickName);
	},
	onShow() {
		let avatarUrl = wx.getStorageSync('avatarUrl');
		let nickName = wx.getStorageSync('SetnickName');
		if (avatarUrl!='') {
			this.setData({
			avatarUrl:avatarUrl
		})
		}
		if (nickName!='') {
			this.setData({
			nickName:nickName
		})
		}
		
		if (typeof this.getTabBar === 'function' && this.getTabBar()) {
			this.getTabBar().setData({
				selected: 1
			})
		}
	},

})